package com.xuzz.study.thread.lock;

/**
 * 模拟死锁
 */
public class DeadLock {

    public static void main(String[] args)
    {
        Object obj1 = new Object();
        Object obj2 = new Object();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj1)
                {
                    System.out.println("threa1 持有obj1");
                    try {
                        Thread.sleep(1000);//为了保证线程thread2启动获得obj2锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj2)
                    {
                        System.out.println("threa1 持有obj2");
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (obj2)
                {
                    System.out.println("threa2 持有obj2");
                    try {
                        Thread.sleep(1000);//为了保证线程thread2启动获得obj2锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (obj1)
                    {
                        System.out.println("threa2 持有obj21");
                    }
                }
            }
        }).start();
    }
}
